﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using WFNetLib.Strings.CryptoService;

namespace WFNetLib
{
    public class SoftVerify_byMachineCode
    {
        public class VerifyMachineCode_DES
        {
            public string ID;
            public string Encode;
        }
        public class VerifyMachineCode_BYTE
        {
            public byte[] ID;
            public byte[] Encode;
            public VerifyMachineCode_BYTE()
            {
                ID = new byte[8];
                Encode = new byte[8];
            }
        }
        public static VerifyMachineCode_DES GetEncode_DES()
        {
            VerifyMachineCode_DES ret = new VerifyMachineCode_DES();
            string str1 = ReadSystemHardDeviceParam.GetHardDisk_SerialNumber().Trim();
            string str2 = ReadSystemHardDeviceParam.GetCPU_SerialNumber().Trim();
            string str3 = ReadSystemHardDeviceParam.GetMacAddrs().Trim();
            string str4 = ReadSystemHardDeviceParam.GetMasterBoard_SerialNumber().Trim();
            byte[] b1 = Encoding.ASCII.GetBytes(str1);
            byte[] b2 = Encoding.ASCII.GetBytes(str2);
            byte[] b3 = Encoding.ASCII.GetBytes(str3);
            byte[] b4 = Encoding.ASCII.GetBytes(str4);
            byte[] ID = new byte[8];
            for (int i = 0; i < 8; i++)
            {
                ID[i] = (byte)(b1[i] ^ b2[i] ^ b3[i] ^ b4[i]);
            }
            lcyHashCal lcy=new lcyHashCal();
            byte[] Encode = lcy.HashCal(ID, 5);
            DESCrypto des = new DESCrypto();
            ret.ID = des.EncryptString(WFNetLib.Strings.StringsFunction.byteToHexStr(ID,""), "wfMaCovf", "Machincd");
            ret.Encode = des.EncryptString(WFNetLib.Strings.StringsFunction.byteToHexStr(Encode, ""), "wfMaCovf", "Machincd");
            return ret;
        }
        public static bool CheckEncode_DES(VerifyMachineCode_DES vc)
        {
            lcyHashCal lcy = new lcyHashCal();
            DESCrypto des = new DESCrypto();
            byte[] ID = WFNetLib.Strings.StringsFunction.strToHexByte(des.DecryptString(vc.ID, "wfMaCovf", "Machincd"),"");
            byte[] x = lcy.HashCal(ID, 5);
            byte[] Encode = WFNetLib.Strings.StringsFunction.strToHexByte(des.DecryptString(vc.Encode, "wfMaCovf", "Machincd"), "");
            for (int i = 0; i < 8; i++)
            {
                if (x[i] != Encode[i])
                    return false;
            }
            return true;
        }
        public static VerifyMachineCode_BYTE GetEncode_BYTE()
        {
            VerifyMachineCode_BYTE ret = new VerifyMachineCode_BYTE();
            string str1 = ReadSystemHardDeviceParam.GetHardDisk_SerialNumber().Trim();
            string str2 = ReadSystemHardDeviceParam.GetCPU_SerialNumber().Trim();
            string str3 = ReadSystemHardDeviceParam.GetMacAddrs().Trim();
            string str4 = ReadSystemHardDeviceParam.GetMasterBoard_SerialNumber().Trim();
            byte[] b1 = Encoding.ASCII.GetBytes(str1);
            byte[] b2 = Encoding.ASCII.GetBytes(str2);
            byte[] b3 = Encoding.ASCII.GetBytes(str3);
            byte[] b4 = Encoding.ASCII.GetBytes(str4);
            for (int i = 0; i < 8; i++)
            {
                ret.ID[i] = (byte)(b1[i] ^ b2[i] ^ b3[i] ^ b4[i]);
            }
            lcyHashCal lcy = new lcyHashCal();
            ret.Encode = lcy.HashCal(ret.ID, 5);            
            return ret;
        }
        public static bool CheckEncode_BYTE(VerifyMachineCode_BYTE vc)
        {
            lcyHashCal lcy = new lcyHashCal();            
            byte[] x = lcy.HashCal(vc.ID, 5);
            for (int i = 0; i < 8; i++)
            {
                if (x[i] != vc.Encode[i])
                    return false;
            }
            return true;
        }
    }
}
